﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using EntityServices;
using Model;
using System.Web.Http.Cors;

namespace WebApp1.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]

    public class 用户Controller : ApiController
    {
        TimeTableOneEntityServicesFactory entityServices = new TimeTableOneEntityServicesFactory();
        [HttpPost]
        [Route("api/user/{uId}/class/{cId}")]
        public int setClazz(int uId, int cId)
        {
            用户 u = entityServices.用户Service.GetById(uId);
            班级 c = entityServices.班级Service.GetById(cId);
            if (u != null && c != null)
            {
                u.班级 = c;
                return entityServices.用户Service.Modify(u);
            }
            else
                return 0;
        }
        // GET api/<controller>
        [HttpPost]
        [Route("api/user/login")]
        public 用户 Login([FromBody]用户 u)
        {
            用户 result = entityServices.用户Service.GetById(u.昵称);
            if (result != null && result.密码 == u.密码)
                return result;
            else
                u.Id = 0;
            return u;
        }


        // GET api/<controller>/5
        public 用户 Get(int id)
        {
            return entityServices.用户Service.GetById(id);
        }

        [HttpGet]
        [Route("api/用户/{uId}/课表")]//公选课
        // GET api/<controller>/5
        public ICollection<课程> GetCourse(int uId)
        {
            int kId = Convert.ToInt32(entityServices.用户Service.GetById(uId).课程Id);
            List<课程> result = new List<课程>();
            result.Add(entityServices.课程Service.GetById(kId));
            return result;
        }

        // POST api/<controller>
        public bool Post([FromBody]用户 value)
        {
            List<用户> temp = entityServices.用户Service.GetList();
            foreach (用户 user in temp)
                if (user.手机号码 == value.手机号码)
                    return false;
            entityServices.用户Service.NewSave(value);
            return true;
        }

        // PUT api/<controller>/5

        public void Put(int id, [FromBody]用户 value)
        {
            if (this.Get(id) != null)
            {
                Delete(id);
                entityServices.用户Service.NewSave(value);

            }
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
            用户 temp = this.Get(id);
            if (temp != null)
                entityServices.用户Service.Remove(temp);
        }

        [HttpPost]
        [Route("api/user/{uId}/验证")]
        public int YnNewUser(int uId)
        {
            int result = Convert.ToInt32(entityServices.用户Service.GetById(uId).班级Id);
            if (result != null)
                return result;
            return 0;
        }

        [HttpGet]
        [Route("api/用户/{uId}/个人信息")]
        public string GetUserInfo(int uId)
        {
            string result = null;

            result += entityServices.用户Service.GetById(uId).昵称 + '#';
            result += entityServices.学院Service.GetById(entityServices.系部Service.GetById(entityServices.用户Service.GetById(uId).班级.系部Id).学院Id).名称 + '#';
            result += entityServices.系部Service.GetById(entityServices.用户Service.GetById(uId).班级.系部Id).系名 + '#';
            result += entityServices.用户Service.GetById(uId).班级.名称;
            return result;
        }
    }
}